package com.netdisk.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.netdisk.entity.File;
import com.netdisk.entity.Share;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.List;

/**
 * 分享Mapper接口
 */
@Mapper
public interface ShareMapper extends BaseMapper<Share> {

    /**
     * 分页查询用户分享的文件
     *
     * @param page 分页参数
     * @param userId 用户ID
     * @return 分页结果
     */
    @Select("SELECT f.* FROM files f JOIN shares s ON f.id = s.file_id WHERE s.user_id = #{userId} AND f.deleted_at IS NULL ORDER BY s.created_at DESC")
    IPage<File> selectShareFiles(Page<File> page, @Param("userId") Integer userId);
    
    /**
     * 根据分享ID查询文件信息
     *
     * @param shareId 分享ID
     * @return 文件信息
     */
    @Select("SELECT f.* FROM files f JOIN shares s ON f.id = s.file_id WHERE s.share_id = #{shareId} AND f.deleted_at IS NULL")
    File selectFileByShareId(@Param("shareId") String shareId);

    /**
     * 根据分享ID获取分享信息
     *
     * @param shareId 分享ID
     * @return 分享实体
     */
    @Select("SELECT * FROM shares WHERE share_id = #{shareId} AND deleted_at IS NULL LIMIT 1")
    Share findByShareId(@Param("shareId") String shareId);
    
    /**
     * 根据文件ID和用户ID查询分享信息
     *
     * @param fileId 文件ID
     * @param userId 用户ID
     * @return 分享实体
     */
    @Select("SELECT * FROM shares WHERE file_id = #{fileId} AND user_id = #{userId} AND deleted_at IS NULL LIMIT 1")
    Share findExistingShare(@Param("fileId") Integer fileId, @Param("userId") Integer userId);

    /**
     * 根据分享ID和用户ID查询分享信息
     *
     * @param shareId 分享ID
     * @param userId 用户ID
     * @return 分享实体
     */
    @Select("SELECT * FROM shares WHERE share_id = #{shareId} AND user_id = #{userId} AND deleted_at IS NULL LIMIT 1")
    Share findByShareIdAndUserId(@Param("shareId") String shareId, @Param("userId") Integer userId);
    
    /**
     * 查询用户的分享列表
     *
     * @param userId 用户ID
     * @param offset 偏移量
     * @param limit 限制条数
     * @return 分享列表
     */
    @Select("SELECT * FROM shares WHERE user_id = #{userId} AND deleted_at IS NULL ORDER BY created_at DESC LIMIT #{offset}, #{limit}")
    List<Share> findSharesByUserId(@Param("userId") Integer userId, @Param("offset") Integer offset, @Param("limit") Integer limit);
    
    /**
     * 带搜索功能的查询用户分享列表
     * 只搜索文件名
     *
     * @param userId 用户ID
     * @param query 查询关键字
     * @param offset 偏移量
     * @param limit 限制条数
     * @return 分享列表
     */
    @Select("SELECT s.* FROM shares s JOIN files f ON s.file_id = f.id " +
            "WHERE s.user_id = #{userId} AND s.deleted_at IS NULL " +
            "AND f.name LIKE CONCAT('%', #{query}, '%') " +
            "ORDER BY s.created_at DESC LIMIT #{offset}, #{limit}")
    List<Share> findSharesByUserIdWithQuery(@Param("userId") Integer userId, @Param("query") String query, @Param("offset") Integer offset, @Param("limit") Integer limit);
    
    /**
     * 计数用户的分享总数
     *
     * @param userId 用户ID
     * @return 总数
     */
    @Select("SELECT COUNT(*) FROM shares WHERE user_id = #{userId} AND deleted_at IS NULL")
    Long countSharesByUserId(@Param("userId") Integer userId);
    
    /**
     * 带搜索功能的计数用户分享总数
     * 只搜索文件名
     *
     * @param userId 用户ID
     * @param query 查询关键字
     * @return 总数
     */
    @Select("SELECT COUNT(*) FROM shares s JOIN files f ON s.file_id = f.id " +
            "WHERE s.user_id = #{userId} AND s.deleted_at IS NULL " +
            "AND f.name LIKE CONCAT('%', #{query}, '%')")
    Long countSharesByUserIdWithQuery(@Param("userId") Integer userId, @Param("query") String query);

    /**
     * 逻辑删除分享
     *
     * @param id 分享ID
     * @return 影响行数
     */
    @Update("UPDATE shares SET deleted_at = NOW() WHERE id = #{id} AND deleted_at IS NULL")
    int markAsDeleted(@Param("id") Integer id);
}